package com.royal.lr.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    public static String trim(String str) {
        return str == null ? "" : str.trim();
    }

    public static boolean isEmpty(String str) {
        str = trim(str);
        return "".equals(str);
    }
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static boolean isChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find())
            flg = true;

        return flg;
    }

    public static String getPreSubStr(String str,String patternStr, int num) {
        String origStr=str;
        int i = 0;
        int lastIndex=0;
        while(i < num) {
            lastIndex = str.indexOf(patternStr);
            System.out.println(lastIndex);
            str = str.substring(lastIndex+1);
            System.out.println(str);
            i++;
        }
        return origStr.replace(str,"");
    }
    private static String getOnlySubStr(String str,String patternStr, int num) {
        String result = "";
        int i = 0;
        while(i < num) {
            int lastFirst = str.lastIndexOf(patternStr);
            result = str.substring(lastFirst) ;
            str = str.substring(0, lastFirst);
            i++;
        }
        return result.substring(1);
    }

    private static int getexpStrNum(String str,String patternStr) {
        String result = str;
        int i = 0;
        while(result.lastIndexOf(patternStr)!=-1){
            int lastFirst = result.lastIndexOf(patternStr);
            // System.out.println(result);
            result = str.substring(0,lastFirst) ;
            i++;
        }
        return i;
    }

    public  String string2Unicode(String string) {

        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); i++) {

            // 取出每一个字符
            char c = string.charAt(i);

            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }
    public static void main(String[] args) {
        System.out.println(replaceBlank("延边朝鲜族\n" +
                "\t自治州"));
    }

    public static String getSuffixSubStr(String str,String patternStr, int num) {
        String result = "";
        int i = 0;
        while(i < num) {
            int lastFirst = str.lastIndexOf(patternStr);
            result = str.substring(lastFirst) + result;
            str = str.substring(0, lastFirst);
            i++;
        }
        return result.substring(1);
    }
}
